<?php
/**
 * Created by PhpStorm.
 * User: jwb
 * Date: 2020/9/17
 * Time: 9:25
 */

namespace App\Services;

use App\Extensions\Exporters\PatentExporter;
use App\Models\Department;
use App\Patent;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Filter;
use Encore\Admin\Grid\Tools\BatchActions;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Tab;

class PatentLibraryService
{

    public function gridColumn(Grid $grid)
    {
        $grid->filter(function (Filter $filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->where(function ($query) {
                    $query->where('patent_sn', 'like', "%{$this->input}%")
                        ->orWhere('patent_name', 'like', "%{$this->input}%")
                        ->orWhere('patent_person', 'like', "%{$this->input}%")
                        ->orWhere('inventor', 'like', "%{$this->input}%");
                }, '关键字')->placeholder('专利号/专利名称/其它');
                $filter->equal('patent_type_id', '专利类型')->select(patent_type_select_option());
                $filter->equal('department_id', '科室')->select(Department::query()->pluck('name','id'));
            });
            $filter->column(1 / 2, function (Filter $filter) {
                $filter->equal('legal_status', __('Patent case'))->select(patent_legal_status());
                $filter->between('apply_date', __('Apply date'))->date();
            });
        });
        $grid->column('patent_sn', __('patent_sn'));
        $grid->column('patent_name', __('专利名称'))->modal('详情', function ($model) {
            return self::detail($model);
        });
        $grid->column('type.name', __('专利类型'));
        $grid->column('legal_status', __('专利状态'));
        $grid->column('apply_date', __('apply_date'));
        $grid->column('department.name', __('科室(部门)'));
        $grid->column('user.name', __('charge_person'));
        $grid->column('inventor', __('inventor'))->hide();
        $grid->column('is_monitor', '年费监控')->display(function ($is_monitor) {
            if(!$is_monitor)return '';
            return '<span class="label label-info">已监控</span>';
        })->hide();
        $grid->column('patent_transaction', __('转移转化'))->display(function () {
            return $this->transaction?'已申请':'';
        })->hide();
        $grid->batchActions(function (BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->disableCreateButton();
        $grid->exporter(new PatentExporter());
        return $grid;
    }

    public static function detail(Patent $patent)
    {
        $tab = new Tab();
        $tab->add('基本信息', Admin::show($patent, function (Show $show) {
            $show->panel()->view('show.tab-panel');
            $show->field('patent_sn', __('Patent sn'));
            $show->field('patent_name', __('Patent name'));
            $show->field('type.name', __('专利类型'));
            $show->field('department.name', __('所属部门'));
            $show->field('patent_person', __('patent_person'));
            $show->field('inventor', __('Inventor'));
            $show->field('apply_date', __('Apply date'));
            $show->field('legal_status', __('Patent case'));
            $show->field('agency', __('代理机构'));
            $show->field('agent', __('代理人'));
        }));
        $unpaids = $patent->unpaid();
        $paids = $patent->paid();
        $tab->add('缴费信息', view('modal.patent-fee')->with(compact('patent', 'unpaids', 'paids')));
        $tab->add('负责人', Admin::show($patent, function (Show $show) {
            $show->panel()->view('show.tab-panel');
            $show->field('user.name', __('姓名'));
            $show->field('user.mobile', __('电话'));
            $show->field('user.email', __('邮箱'));
        }));
        return $tab;
    }
}
